<div class="layui-card">
    <div class="layui-card-header">
        <h2 class="header-title">视频管理</h2>
        <span class="layui-breadcrumb pull-right">
          <a href="#!console">首页</a>
          <a><cite>视频管理</cite></a>
        </span>
    </div>
    <div class="layui-card-body">
        <div class="layui-form toolbar">
            搜索：
            <select id="video-search-key">
                <option value="">-请选择-</option>
                <option value="fileName">视频名称</option>
                <option value="type">类型</option>
                <option value="companyName">公司名称</option>
            </select>&emsp;
            <input id="video-search-value" class="layui-input search-input" type="text" placeholder="输入关键字"/>&emsp;
            <button id="user-btn-search" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
            <button id="video-btn-add" class="layui-btn icon-btn"><i class="layui-icon">&#xe654;</i>上传视频</button>
        </div>

        <table class="layui-table" id="video-table" lay-filter="video-table"></table>
    </div>
</div>


<!-- 表格操作列 -->
<script type="text/html" id="video-table-bar">
    <a class="layui-btn layui-btn-xs" lay-event="reset">删除</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="user-tpl-state">
    <input type="checkbox" lay-filter="user-tpl-state" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.enabled==true?'checked':''}}/>
</script>

<script>
    layui.use(['form', 'table', 'util', 'config', 'admin'], function () {
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var layer = layui.layer;
        var util = layui.util;
        var admin = layui.admin;

        //if( admin.hasPerm('user:add') ) {
        //   $('#btnAdd').hide();
        //}

        // 添加按钮绑定权限
        if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#video-btn-add').hide();
        }

        // 导出按钮绑定权限
        if(!admin.hasPerm('user:post/users/saveOrUpdate')) {
            $('#user-btn-export').hide();
        }

        // 渲染表格
        table.render({
            elem: '#video-table',
            // url: config.base_server + 'user_query.json',
            url: config.base_server + 'api-videos/video/findSomeVideo'+'?access_token='
                +config.getToken().access_token+'&userId='+config.getUser().id,
            method: 'post',
            where: {
                // access_token: config.getToken().access_token
            },
            page: true,
            cols: [[
                {type: 'numbers'},
                {field: 'id', sort: true, title: 'id'},
                {field: 'fileName', sort: true, title: '视屏名称'},
                {field: 'fileUrl', sort: true, title: '视屏播放地址'},
                {field: 'vid', sort: true, title: 'vid'},
                {field: 'date', sort: true, title: '上传日期'},
                {field: 'type', sort: true, title: '类型'},
                {field: 'companyName', sort: true, title: '公司'},
                // {
                //     sort: true, templet: function (d) {
                //         return util.toDateString(d.createTime);
                //     }, title: '创建时间'
                // },
                {align: 'center', toolbar: '#video-table-bar', title: '操作',width: 150}
            ]]
        });

        // 添加按钮点击事件
        $('#video-btn-add').click(function () {
            showEditModel();
        });

        // 工具条点击事件
        table.on('tool(video-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'reset') { // 重置密码
                layer.confirm('确定要删除此视频吗？', function (i) {
                    layer.close(i);
                    layer.load(2);
                    admin.req('api-videos/video/deleteVideo?vid='+obj.data.vid, {}, function (data) {
                        layer.closeAll('loading');
                        if (data.code == 0) {
                            layer.msg(data.msg, {icon: 1, time: 500});
                        } else {
                            layer.msg(data.msg, {icon: 2, time: 500});
                        }
                    }, 'POST');
                });
            }
        });

        //显示表单弹窗
        var showEditModel = function (data) {
            admin.putTempData('t_video', data);
            var title = data ? '修改视频' : '添加视频';
            admin.popupCenter({
                title: title,
                path: 'pages/system/video_form.html',
                finish: function () {
                    table.reload('video-table', {});
                }
            });
        };

        // 搜索按钮点击事件
        $('#user-btn-search').click(function () {
            var key = $('#video-search-key').val();
            var value = $('#video-search-value').val();
            table.reload('video-table', {where: {userId:config.getUser().id,searchKey: key, searchValue: value}});
        });

        // // 修改user状态
        // form.on('switch(user-tpl-state)', function (obj) {
        //     layer.load(2);
        //     admin.req('api-user/users/updateEnabled', {
        //         id: obj.elem.value,
        //         enabled: obj.elem.checked ? true : false
        //     }, function (data) {
        //         layer.closeAll('loading');
        //         if (data.code == 0) {
        //             layer.msg(data.msg, {icon: 1, time: 500});
        //             //table.reload('table-user', {});
        //         } else {
        //             layer.msg(data.msg, {icon: 2, time: 500});
        //             $(obj.elem).prop('checked', !obj.elem.checked);
        //             form.render('checkbox');
        //         }
        //     }, 'GET');
        // });
    });
</script>